package com.ex01.ylw;

import java.io.IOException;
import java.io.InputStream;

/**
 * 目前主要用来解析Socket里面InputStream的URI
 * Created by yangliwei on 2017/7/19.
 */
public class Request {
    private InputStream in;
    private String uri;

    public Request(InputStream in) {
        this.in = in;
    }

    public void parse() {
        StringBuilder sb = new StringBuilder();
        byte[] b = new byte[2048];
        int i = 0;
        try {
            i = in.read(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int j = 0; j < i; j++) {
            sb.append((char) b[j]);
        }
        parseURI(sb);
    }

    private void parseURI(StringBuilder sb) {
        //别人的代码这里每调用一个函数，就要对返回参数进行判断
        int i = sb.indexOf(" ");
        int j = sb.indexOf(" ", i + 1);
        if (i != -1 && j > i) {
            uri = sb.substring(i + 1, j);
        }
    }

    public String getUri() {
        return uri;
    }
}
